<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .active {
        background-color: antiquewhite;
      }

      li {
        margin: 5px;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <ul>
      <li data-item>css</li>
      <li data-item>html</li>
      <li data-item>js</li>
      <li data-item>git</li>
    </ul>
    <script>
      document.onmousedown = function (e) {
        // 只对li标签取消文字选中行为
        if (e.target.dataset.item !== undefined) {
          e.preventDefault();
        }
      };
      document.onclick = function (e) {
        if (e.target.dataset.item === undefined) return;
        // 单选
        if (e.ctrlKey === false) {
          document
            .querySelectorAll(".active")
            .forEach((el) => el.classList.remove("active"));
        }
        e.target.classList.toggle("active");
      };
    </script>
  </body>
</html>
